package com.hospital.pharmacy.impl;

import com.alibaba.fastjson.JSON;
import com.hospital.pharmacy.dao.InventoryMapper;
import com.hospital.pharmacy.dao.MedicineMapper;
import com.hospital.pharmacy.entity.Inventory;
import com.hospital.pharmacy.entity.Medicine;
import com.hospital.pharmacy.service.InventoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: Hospital
 * @description: 库存实现
 * @author: 杨曼珍
 * @create: 2019-01-07 11:31
 **/
@Service
public class InventoryServiceImpl implements InventoryService {

    @Autowired
    InventoryMapper inventoryMapper;
    @Autowired
    MedicineMapper medicineMapper;


    //显示库存信息
    public Map getAll(Inventory inventory){
        Map<String,String>map;

            map=new HashMap<>();
        Medicine medicine=medicineMapper.selectByPrimaryKey(inventory.getIvtrMdcId());
           map.put("id",inventory.getId().toString());
            map.put("mdcName",medicine.getMdcName());
            map.put("mdcSpec",medicine.getMdcSpec());
            map.put("mdcUnit",medicine.getMdcUnit());
            map.put("ivtrCount",inventory.getIvtrCount().toString());
            map.put("ivtrGuaranteeDate",new SimpleDateFormat("yyyy-MM-dd").format(inventory.getIvtrGuaranteeDate()));
            map.put("ivtrRemark",inventory.getIvtrRemark());

        return map;
    }
    @Override
    public String getAllInventory(){
        List<Inventory>inventoryList=inventoryMapper.selectAllInventory();
        List<Map<String,String>>resaultmap=new ArrayList<>();
        for (Inventory inventory:inventoryList){
            resaultmap.add(getAll(inventory));
        }
        return JSON.toJSONString(resaultmap);
    }
    @Override
    public String getInventoryByMedName(String med_name){
        List<Medicine>medicines=medicineMapper.selectByMdcName(med_name);
        List<Map<String,String>>resaultmap=new ArrayList<>();
        for (Medicine medicine:medicines){
            Inventory inventory=inventoryMapper.selectByMdcId(medicine.getId());
            resaultmap.add(getAll(inventory));
        }
        return JSON.toJSONString(resaultmap);
    }


}
